package com.shigu.constant.service;

import com.opentae.data.mall.beans.ManagerPageConstant;
import com.opentae.data.mall.examples.ManagerPageConstantExample;
import com.opentae.data.mall.interfaces.ManagerPageConstantMapper;
import com.shigu.PageResponse;
import com.shigu.component.utils.BeanMapper;
import com.shigu.constant.bo.AddConstantsRequest;
import com.shigu.constant.bo.ModifyConstantsRequest;
import com.shigu.constant.bo.MyconstantsRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminConstantService {
    @Autowired
    private ManagerPageConstantMapper managerPageConstantMapper;

    public void addConstant(AddConstantsRequest bo){
        managerPageConstantMapper.insertSelective(BeanMapper.map(bo, ManagerPageConstant.class));
    }

    public void delConstant(Long id){
        managerPageConstantMapper.deleteByPrimaryKey(id);
    }

    public void modifyConstant(ModifyConstantsRequest bo){
        managerPageConstantMapper.updateByPrimaryKeySelective(BeanMapper.map(bo, ManagerPageConstant.class));
    }

    public PageResponse<ManagerPageConstant> selList(MyconstantsRequest bo){
        ManagerPageConstantExample example=new ManagerPageConstantExample();
        if (StringUtils.isNotEmpty(bo.getPageUri())) {
            example.createCriteria().andPageUriEqualTo(bo.getPageUri());
        }
        example.setStartIndex((bo.getPage()-1)*bo.getSize());
        example.setEndIndex(bo.getSize());
        example.setOrderByClause("constant_id desc");
        PageResponse<ManagerPageConstant> res=new PageResponse<>();
        res.setTotal(managerPageConstantMapper.countByExample(example));
        res.setData(managerPageConstantMapper.selectByConditionList(example));
        return res.success();
    }
}
